package com.example.demo.study.supermarket;

import java.time.LocalDate;
import java.util.*;

// 会员管理类
public class MemberMgr {
    // 所有会员（卡） --- 因为会员 会比较多 --- 所以会用到集合 -- list
    List<Member> memberList = new ArrayList<Member>();

    // 用户输入内容
    static Scanner scanner = new Scanner(System.in);
    // 输入欢迎菜单
    public void menu(){
        System.out.println("***********欢迎使用超市会员管理系统*****************************");
        System.out.println("1.开卡  2.修改密码  3.积分累计  4.积分兑换  5.查询剩余积分  6.退出");
        System.out.println("************************************************************");
        System.out.print("请输入您的选择：");
    }

    // 框架搭建流程
    public void start(){
        Boolean flag = true;
            do{
                menu();
                int choice = scanner.nextInt();
                switch (choice){
                    case 1:
                        // 开卡
                        System.out.println("开卡：");
                        openCard();
                        break;
                    case 2:
                        // 修改密码
                        System.out.println("修改密码：");
                        if(modifyPassword() == true){
                            System.out.println("新密码设置成功");
                        }else {
                            System.out.println("新密码设置失败");
                        }
                        break;
                    case 3:
                        // 积分累计
                        System.out.println("积分累计：");
                        accumulateScore();
                        break;
                    case 4:
                        // 积分兑换
                        System.out.println("积分兑换：");
                        integralExchange();
                        break;
                    case 5:
                        // 查询剩余积分
                        System.out.println("查询剩余积分：");
                        integralInquiry();
                        break;
                    case 6:
                        // 退出
                        System.out.println("退出：");
                        flag = false;
                        break;
                    default:
                        System.out.println("输入有误，请重新输入！");
                        break;
                }
            }while (flag);
    }

    // 开卡方法
    public void openCard(){

        Member member = new Member();
        System.out.print("请输入注册姓名：");
        member.setName(scanner.next());
        System.out.print("请输入注册密码：");
        String pwd;
        Boolean flag = true;
        do{
            pwd = scanner.next();
            if(pwd.length()<6){
                System.out.println("密码长度小于6位，请重新输入注册密码！");
                flag = true;
            }else {
                flag = false;
                member.setPassword(pwd);
            }
        }while (flag);
        // 卡号
        member.setCardId(createId());
        member.setScore(100);
        // 生成今当日期
        LocalDate localDate = LocalDate.now();
        String regDate = localDate.getMonthValue()+"月"+localDate.getDayOfMonth()+"日";
        member.setRegisterDate(regDate);
        memberList.add(member);
        System.out.println("开通会员卡成功！系统赠送您100积分，你的会员卡号为："+member.getCardId());
        System.out.println("会员注册人数："+memberList.size());

    }
    // 系统随机生成一个会员卡号
    public int createId(){
        Random random = new Random();
        int cardNo =  random.nextInt(99999999);
        for (Member member: memberList) {
            if(member.getCardId() == cardNo){
                cardNo =  random.nextInt(99999999);
            }
        }
        return cardNo;
    }
    // 修改密码方法
    public Boolean modifyPassword(){
        Boolean flag = true;
        Member member = new Member();
        System.out.print("请输入会员卡号：");
        member.setCardId(scanner.nextInt());
        System.out.print("请输入原密码：");
        member.setPassword(scanner.next());
        member = hasMember(member); // 如果找到，就是找到会员对象，如果没找到，就是null
        if(member !=null){
           // 如果找到了，才允许修改密码
            System.out.print("请输入新密码：");
            String pwd;
            Boolean flagPwd = true;
            do{
                pwd = scanner.next();
                if(pwd.length()<6){
                    System.out.println("密码长度小于6位，请重新输入注册密码！");
                    flagPwd = true;
                }else {
                    member.setPassword(pwd);
                    flagPwd = false;
                    flag = true;
                }
            }while (flagPwd);

        }else {
            System.out.println("会员卡号不存在，请重新输入！");
            flag = false;
        }
        return flag;

    }
    // 验证会员是否存在
    // 查询条件 --- 会员卡号和密码 -- 封装在参数中转过来
    public Member hasMember(Member m){
        Member member = null;
        for (Member memberItem: memberList){
            if(memberItem.getCardId()== m.getCardId() && memberItem.getPassword().equals(m.getPassword())){
                member = memberItem;
                break;
            }
        }
        return member;
    }
    // 积分累计
    public void accumulateScore(){
        Member member = new Member();
        System.out.println("请输入您的会员卡号：");
        member.setCardId(scanner.nextInt());
        System.out.println("请输入您的会员卡密码：");
        member.setPassword(scanner.next());

        member = hasMember(member); // 如果找到，就是找到会员对象，如果没找到，就是null

        if(member!=null){
            System.out.print("请输入您要积分的数量：");
            int score =scanner.nextInt();
            member.setScore(member.getScore()+score);
            System.out.println("积分累计成功！累计共金额："+member.getScore());
        }else {
            System.out.println("会员卡号不存在，请重新输入！");
        }

    }
    // 积分兑换方法
    public void integralExchange(){
        Member member = new Member();
        System.out.println("请输入您的会员卡号：");
        member.setCardId(scanner.nextInt());
        System.out.println("请输入您的会员卡密码：");
        member.setPassword(scanner.next());

        member = hasMember(member); // 如果找到，就是找到会员对象，如果没找到，就是null
        if(member!=null){
            System.out.println("请输入您需要兑换使用的积分（100积分抵用0.1，不足100积分不做抵用）：");
            int score = scanner.nextInt();
            if(member.getScore()>=score){
                member.setScore(member.getScore()-score);
                System.out.println("积分兑换成功！");
                System.out.println("您的消费金额中使用会员积分抵消："+member.getScore()/100*0.1+"元");
            }
        }else {
            System.out.println("积分不足，兑换失败！");
        }

    }
    // 积分查询方法
    public void integralInquiry(){
        Member member = new Member();
        System.out.println("请输入您的会员卡号：");
        member.setCardId(scanner.nextInt());
        System.out.println("请输入您的会员卡密码：");
        member.setPassword(scanner.next());

        member = hasMember(member); // 如果找到，就是找到会员对象，如果没找到，就是null
        if(member !=null){
            System.out.println("姓名\t\t会员卡号\t\t剩余积分\t开卡日期");
            System.out.println(member.getName()+"\t"+member.getCardId()+"\t"+member.getScore()+"\t"+member.getRegisterDate());
        }else {
            System.out.println("查询会员信息不存在");
        }

    }



}
